<template>
  <section class="container">

    <div class="loginbody">
      <div class="logindata">
        <div class="logintext">
          <h2>找回密码</h2>
        </div>
        <div class="formdata">
          <el-form ref="form" :model="user" :rules="rules">
            <el-form-item prop="username">
              <el-input
                v-model="user.account"
                clearable
                placeholder="请输入账号"
              ></el-input>
            </el-form-item>

            <el-form-item prop="username">
              <el-input
                v-model="user.mail"
                clearable
                placeholder="请输入邮箱"
              ></el-input>
            </el-form-item>

            <el-form-item prop="username">
              <el-input
                @blur="isCode"
                style="width: 280px"
                v-model="code"
                clearable
                placeholder="请输入验证码"
              ></el-input>
              <el-button v-if="!sendMsgDisabled" @click="count">获取验证码</el-button>
              <el-button v-if="sendMsgDisabled" disabled>{{ time+'秒后获取' }}</el-button>
            </el-form-item>



            <el-form-item prop="password">
              <el-input
                v-show="isCorrect"
                v-model="user.password"
                clearable
                placeholder="请输入新密码"
                show-password
              ></el-input>

              <br><br>
              <el-form-item prop="password">
                <el-button type="primary" style="width: 400px" @click="summit">确认</el-button>
              </el-form-item>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
import AppLogo from '~/components/AppLogo.vue'
import userClient from "~/api/userClient";
export default {
  components: {
    AppLogo
  },

  data() {
    return {
      isCorrect:false,
      time: 60,
      sendMsgDisabled: false,
      checked: false,
      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        code:[
          { required: true, message: "请输入密码", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ]
      },
      code:"",
      user:{
        account:"",
        mail:"",
        password:"",
      }
    };
  },
  created() {

  },
  mounted() {

  },
  methods: {
    count(){
      const that = this
      that.sendMsgDisabled = true
      const interval = window.setInterval(function() {
        if ((that.time--) <= 0) {
          that.time = 60
          that.sendMsgDisabled = false
          window.clearInterval(interval)
        }
      }, 1000)

      userClient.sendMail(this.user.mail)
      .then(response=>{
        this.$message({
          type:"success",
          message:"发送成功"
        })
      })
    },
    isCode(){
      userClient.getCode(this.user.mail)
      .then(response=>{
        console.log(response.data.data.code)
        if(this.code == response.data.data.code){
          this.isCorrect = true
        }else{
          this.$message({
            type:"error",
            message:"请输入正确的验证码"
          })
        }
      })
      .catch(err=>{
        console.log(err)
      })
    },
    summit(){
      userClient.retPassword(this.user)
      .then(response=>{
        if(response.data.code == 20000){
          this.$message({
            type:"success",
            message:"成功"
          })
          this.$router.push({path:"login"})
        }else {
          this.$message({
            type:"error",
            message:"请检查邮箱或账号是否正确"
          })
        }

      })
    }

  },
}
</script>

<style scoped>
.loginbody {
  width: 100%;
  height: 100%;
  min-width: 1000px;
  /*background-image: url("https://edu-remains.oss-cn-beijing.aliyuncs.com/2022/08/03/3956141e5fc544648a5c7b460f6260f1010a0e5d9ed02ca801211d5311891c.jpg@1280w_1l_2o_100sh.jpg");*/
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  line-height: 100%;
  padding-top: 150px;
}

.logintext {
  margin-bottom: 20px;
  line-height: 50px;
  text-align: center;
  font-size: 30px;
  font-weight: bolder;
  color: black;
}

.logindata {
  width: 400px;
  height: 300px;
  transform: translate(-50%);
  margin-left: 50%;
}

.tool {
  display: flex;
  justify-content: space-between;
  color: #606266;
}

.butt {
  margin-top: 10px;
  text-align: center;
}

.shou {
  cursor: pointer;
  color: #606266;
}

/*ui*/
/* /deep/ .el-form-item__label {
  font-weight: bolder;
  font-size: 15px;
  text-align: left;
}

/deep/ .el-button {
  width: 100%;
  margin-bottom: 10px;

} */
</style>
